﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace QiBu.Api.Helper.Common
{
    public static class IPHelper
    {
        /// <summary>
        /// 获取IP地址
        /// </summary>
        public static string GetIPAddress()
        {
            var resqust = HttpContext.Current.Request;
            if (resqust == null)
            {
                throw new Exception("resqust is null!");
            }
            string userIP;
            // 如果使用代理，获取真实IP
            if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != "")
            {
                userIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
            else
            {
                userIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            }
            if (string.IsNullOrWhiteSpace(userIP))
            {
                userIP = HttpContext.Current.Request.UserHostAddress;
            }
            return userIP;
        }
    }
}
